package com.tqjc.common.websocket.service.impl;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.qy.led.SendBuffer;
import com.tqjc.area.entity.Roller;
import com.tqjc.area.service.RollerService;
import com.tqjc.common.consts.NormalConst;
import com.tqjc.common.faulthandle.entity.FaultRecord;
import com.tqjc.common.faulthandle.service.FaultRecordService;
import com.tqjc.common.websocket.service.MessageSendService;
import com.tqjc.execute.cache.TaskOtherStatus;
import com.tqjc.execute.cache.TaskStatus;
import com.tqjc.execute.cache.entity.DeviceStatus;
import com.tqjc.execute.cache.entity.WorkStatus;
import com.tqjc.execute.crane.entity.CraneInfo;
import com.tqjc.execute.crane.service.CraneInfoService;
import com.tqjc.execute.thread.aysn.AysnSaveFaultInfoThread;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.execute.utils.PlcOperator;
import com.tqjc.execute.utils.PublicResult;
import com.tqjc.execute.work.CraneWorkFactory;
import com.tqjc.plccontrol.entity.ConnectInfo;
import com.tqjc.plccontrol.service.ConnectInfoService;
import com.tqjc.plccontrol.service.PointInfoService;
import com.tqjc.plccontrol.utils.PlcResult;
import com.tqjc.system.core.redis.TaskRedisApi;
import com.tqjc.system.core.threadpool.ThreadPoolManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;


@Service
@Slf4j
public class MessageSendServiceImpl implements MessageSendService {
    @Resource
    private SimpMessageSendingOperations simpMessageSendingOperations;

    @Resource
    private ConnectInfoService connectInfoService;

    @Resource
    PlcOperator plcOperator;

    @Resource
    TaskStatus taskStatus;

    @Resource
    TaskOtherStatus taskOtherStatus;

    @Resource
    CraneWorkFactory craneWorkFactory;

    @Resource
    RollerService rollerService;

    @Resource
    TaskRedisApi taskRedisApi;

    @Resource
    CraneInfoService craneInfoService;

    @Value("${server_host}")
    private String serverHost;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FaultRecordService faultRecordService;

    /*static int val=0;

    static boolean addFlag=true;*/

    @Override
    @Scheduled(cron = "0/1 * *  * * ? ")
    @Async
    public void sendPlcData() {
        for(String craneType:NormalConst.CRANE_LIST){
            Map<String, String> datas = new HashMap<>();

            try{

                String val=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车位置",false);
                /*if(val>=1000){
                    addFlag=false;
                }
                if(val<=0){
                    addFlag=true;
                }
                if(addFlag){
                    val=val+10;
                }
                if(!addFlag){
                    val=val-10;
                }*/

                datas.put("x",val+"");
                String magnetBlock1=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程磁铁1",false);
                datas.put("magnetBlock1",magnetBlock1);
                String magnetBlock2=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程磁铁2",false);
                datas.put("magnetBlock2",magnetBlock2);
                String magnetBlock3=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程磁铁3",false);
                datas.put("magnetBlock3",magnetBlock3);

                String magnetBlock4=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程磁铁4",false);
                datas.put("magnetBlock4",magnetBlock4);

                String magnetBlock5=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程磁铁5",false);
                datas.put("magnetBlock5",magnetBlock5);

                String magnetBlock6=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程磁铁6",false);
                datas.put("magnetBlock6",magnetBlock6);

                String magnetBlock7=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程磁铁7",false);
                datas.put("magnetBlock7",magnetBlock7);

                String magnetBlock8=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程磁铁8",false);
                datas.put("magnetBlock8",magnetBlock8);

                String magnetValue=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程手动磁力给定",false);
                datas.put("magnetValue",magnetValue);

                String magnetBlockVal1=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吸盘1电流",false);
                datas.put("magnetBlockVal1",magnetBlockVal1);

                String magnetBlockVal2=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吸盘2电流",false);
                datas.put("magnetBlockVal2",magnetBlockVal2);

                String magnetBlockVal3=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吸盘3电流",false);
                datas.put("magnetBlockVal3",magnetBlockVal3);

                String magnetBlockVal4=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吸盘4电流",false);
                datas.put("magnetBlockVal4",magnetBlockVal4);

                String magnetBlockVal5=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吸盘5电流",false);
                datas.put("magnetBlockVal5",magnetBlockVal5);

                String magnetBlockVal6=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吸盘6电流",false);
                datas.put("magnetBlockVal6",magnetBlockVal6);

                String magnetBlockVal7=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吸盘7电流",false);
                datas.put("magnetBlockVal7",magnetBlockVal7);

                String magnetBlockVal8=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吸盘8电流",false);
                datas.put("magnetBlockVal8",magnetBlockVal8);



                String zhDY=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"主电源反馈",false);
                if(zhDY.equalsIgnoreCase("true")){
                    this.saveFaultInfo("主电源反馈异常","1",craneType);
                }
                datas.put("zhDY",zhDY);
                String zhJChQ=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"主接触器反馈",false);
                if(zhJChQ.equalsIgnoreCase("true")){
                    this.saveFaultInfo("主接触器反馈异常","1",craneType);
                }
                datas.put("zhJChQ",zhJChQ);
                String zhCh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"重锤",false);
                if(zhCh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("重锤反馈异常","1",craneType);
                }
                datas.put("zhCh",zhCh);
                String dChDY=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车电源反馈",false);
                if(dChDY.equalsIgnoreCase("true")){
                    this.saveFaultInfo("大车电源反馈异常","1",craneType);
                }
                datas.put("dChDY",dChDY);
                String qShDY=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"起升电源反馈",false);
                if(qShDY.equalsIgnoreCase("true")){
                    this.saveFaultInfo("起升电源反馈异常","1",craneType);
                }
                datas.put("qShDY",qShDY);
                String mXW1=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"门限位1",false);
                if(mXW1.equalsIgnoreCase("true")){
                    this.saveFaultInfo("门限位1异常","1",craneType);
                }
                datas.put("mXW1",mXW1);
                String mXW2=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"门限位2",false);
                if(mXW2.equalsIgnoreCase("true")){
                    this.saveFaultInfo("门限位2异常","1",craneType);
                }
                datas.put("mXW2",mXW2);
                String chShTX=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"横移车通信故障",false);
                if(chShTX.equalsIgnoreCase("true")){
                    this.saveFaultInfo("横移车通信故障","1",craneType);
                }
                datas.put("chShTX",chShTX);
                String dJTX=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"电磁吊具通信故障",false);
                if(dJTX.equalsIgnoreCase("true")){
                    this.saveFaultInfo("电磁吊具通信故障","1",craneType);
                }
                datas.put("dJTX",dJTX);
                String shCTX=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"上层行车通信故障",false);
                if(shCTX.equalsIgnoreCase("true")){
                    this.saveFaultInfo("上层行车通信故障","1",craneType);
                }
                datas.put("shCTX",shCTX);

                String bP1GZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车变频1故障",false);
                if(bP1GZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("大车变频1故障","2",craneType);
                }
                datas.put("bP1GZh",bP1GZh);
                if("true".equalsIgnoreCase(bP1GZh)){
                    String bP1GZhCode=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车变频1故障码",false);
                    datas.put("bP1GZhCode",bP1GZhCode);
                }
                String bP2GZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车变频2故障",false);
                datas.put("bP2GZh",bP2GZh);
                if(bP2GZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("大车变频2故障","2",craneType);
                }

                if("true".equalsIgnoreCase(bP2GZh)){
                    String bP2GZhCode=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车变频2故障码",false);
                    datas.put("bP2GZhCode",bP2GZhCode);
                }
                String zXW=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车左极限",false);
                datas.put("zXW",zXW);
                if(zXW.equalsIgnoreCase("true")){
                    this.saveFaultInfo("大车左极限故障","2",craneType);
                }
                String yXW=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车右极限",false);
                datas.put("yXW",yXW);
                if(yXW.equalsIgnoreCase("true")){
                    this.saveFaultInfo("大车右极限故障","2",craneType);
                }

                String qShBP1GZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"起升变频1故障",false);
                if(qShBP1GZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("起升变频1故障","3",craneType);
                }
                datas.put("qShBP1GZh",qShBP1GZh);
                if("true".equalsIgnoreCase(qShBP1GZh)){
                    String qShBP1GZhCode=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"起升变频1故障码",false);
                    datas.put("qShBP1GZhCode",qShBP1GZhCode);
                }
                String qShBP2GZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"起升变频2故障",false);
                if(qShBP2GZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("起升变频2故障","3",craneType);
                }
                datas.put("qShBP2GZh",qShBP2GZh);
                if("true".equalsIgnoreCase(qShBP2GZh)){
                    String qShBP2GZhCode=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"起升变频2故障码",false);
                    datas.put("qShBP2GZhCode",qShBP2GZhCode);
                }
                String dX1ShXW=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"1#导向柱上限位",false);
                if(dX1ShXW.equalsIgnoreCase("true")){
                    this.saveFaultInfo("1#导向柱上限位故障","3",craneType);
                }
                datas.put("dX1ShXW",dX1ShXW);
                String dX2ShXW=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"2#导向柱上限位",false);
                if(dX2ShXW.equalsIgnoreCase("true")){
                    this.saveFaultInfo("2#导向柱上限位","3",craneType);
                }
                datas.put("dX2ShXW",dX2ShXW);
                String dCDJJ1=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"电磁吊接近停止1",false);
                if(dCDJJ1.equalsIgnoreCase("true")){
                    this.saveFaultInfo("电磁吊接近停止1故障","3",craneType);
                }
                datas.put("dCDJJ1",dCDJJ1);
                String dCDJJ2=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"电磁吊接近停止2",false);
                if(dCDJJ2.equalsIgnoreCase("true")){
                    this.saveFaultInfo("电磁吊接近停止2故障","3",craneType);
                }
                datas.put("dCDJJ2",dCDJJ2);
                String dCDJX1=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"电磁吊极限位1",false);
                if(dCDJX1.equalsIgnoreCase("true")){
                    this.saveFaultInfo("电磁吊极限位1故障","3",craneType);
                }
                datas.put("dCDJX1",dCDJX1);
                String dCDJX2=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"电磁吊极限位2",false);
                datas.put("dCDJX2",dCDJX2);
                if(dCDJX2.equalsIgnoreCase("true")){
                    this.saveFaultInfo("电磁吊极限位2故障","3",craneType);
                }

                String chDXTGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"充电系统故障",false);
                if(chDXTGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("充电系统故障","4",craneType);
                }
                datas.put("chDXTGZh",chDXTGZh);
                String lCXTGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"励磁系统故障",false);
                if(lCXTGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("励磁系统故障","4",craneType);
                }
                datas.put("lCXTGZh",lCXTGZh);
                String chDTXGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"充电通信故障",false);
                if(chDTXGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("充电通信故障","4",craneType);
                }
                datas.put("chDTXGZh",chDTXGZh);
                String dDY=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"低电压报警",false);
                if(dDY.equalsIgnoreCase("true")){
                    this.saveFaultInfo("低电压报警故障","4",craneType);
                }
                datas.put("dDY",dDY);
                String MFMCTXGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"MFMC通信故障",false);
                if(MFMCTXGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("MFMC通信故障","4",craneType);
                }
                datas.put("MFMCTXGZh",MFMCTXGZh);
                String fZ12TXGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"分组12通信故障",false);
                if(fZ12TXGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("分组12通信故障","4",craneType);
                }
                datas.put("fZ12TXGZh",fZ12TXGZh);
                String fZ34TXGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"分组34通信故障",false);
                if(fZ34TXGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("分组34通信故障","4",craneType);
                }
                datas.put("fZ34TXGZh",fZ34TXGZh);
                String dDL=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"低电流报警",false);
                if(dDL.equalsIgnoreCase("true")){
                    this.saveFaultInfo("低电流报警故障","4",craneType);
                }
                datas.put("dDL",dDL);
                String fZ56TXGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"分组56通信故障",false);
                if(fZ56TXGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("分组56通信故障","4",craneType);
                }
                datas.put("fZ56TXGZh",fZ56TXGZh);
                String fZ78TXGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"分组78通信故障",false);
                if(fZ78TXGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("分组78通信故障","4",craneType);
                }
                datas.put("fZ78TXGZh",fZ78TXGZh);
                String shNWDD=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"使能位掉电",false);
                if(shNWDD.equalsIgnoreCase("true")){
                    this.saveFaultInfo("使能位掉电","4",craneType);
                }
                datas.put("shNWDD",shNWDD);
                String xLChSh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吸料超时报警",false);
                if(xLChSh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("吸料超时报警","4",craneType);
                }
                datas.put("xLChSh",xLChSh);
                String dChQSh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"电池缺失",false);
                if(dChQSh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("电池缺失","4",craneType);
                }
                datas.put("dChQSh",dChQSh);
                String dChDYD=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"电池电压低",false);
                if(dChDYD.equalsIgnoreCase("true")){
                    this.saveFaultInfo("电池电压低","4",craneType);
                }
                datas.put("dChDYD",dChDYD);
                String chDGY=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"充电过压",false);
                if(chDGY.equalsIgnoreCase("true")){
                    this.saveFaultInfo("充电过压","4",craneType);
                }
                datas.put("chDGY",chDGY);
                String lCGL=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"励磁过流",false);
                if(lCGL.equalsIgnoreCase("true")){
                    this.saveFaultInfo("励磁过流","4",craneType);
                }
                datas.put("lCGL",lCGL);
                String chDGL=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"充电过流",false);
                if(chDGL.equalsIgnoreCase("true")){
                    this.saveFaultInfo("充电过流","4",craneType);
                }
                datas.put("chDGL",chDGL);
                String jLGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"交流故障",false);
                if(jLGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("交流故障","4",craneType);
                }
                datas.put("jLGZh",jLGZh);
                String lCZhBGZh=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"励磁主板故障",false);
                if(lCZhBGZh.equalsIgnoreCase("true")){
                    this.saveFaultInfo("励磁主板故障","4",craneType);
                }
                datas.put("lCZhBGZh",lCZhBGZh);
                String bCTR=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"保磁投入",false);
                if(bCTR.equalsIgnoreCase("true")){
                    this.saveFaultInfo("保磁投入故障","4",craneType);
                }
                datas.put("bCTR",bCTR);

                String ycDY=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"远程模式",false);
                datas.put("ycDY",ycDY);

                String zdDY=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"自动模式",false);
                datas.put("zdDY",zdDY);
                String dmmxw1=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"地面门限位1",false);
                if(dmmxw1.equalsIgnoreCase("true")){
                    this.saveFaultInfo("地面门限位1故障","4",craneType);
                }
                datas.put("dmmxw1",dmmxw1);

                String dmmxw2=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"地面门限位2",false);
                if(dmmxw2.equalsIgnoreCase("true")){
                    this.saveFaultInfo("地面门限位2故障","4",craneType);
                }
                datas.put("dmmxw2",dmmxw2);

                String dmmxw3=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"地面门限位3",false);
                if(dmmxw3.equalsIgnoreCase("true")){
                    this.saveFaultInfo("地面门限位3故障","4",craneType);
                }
                datas.put("dmmxw3",dmmxw3);
                String dcmxw=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"登车门限位",false);
                if(dcmxw.equalsIgnoreCase("true")){
                    this.saveFaultInfo("登车门限位故障","4",craneType);
                }
                datas.put("dcmxw",dcmxw);


                String BMQYC=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"编码器异常",false);
                if(BMQYC.equalsIgnoreCase("true")){
                    this.saveFaultInfo("编码器异常","4",craneType);
                }
                datas.put("BMQYC",BMQYC);

                String QSBMQ1TXGZ=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"起升编码器1通讯故障",false);
                if(QSBMQ1TXGZ.equalsIgnoreCase("true")){
                    this.saveFaultInfo("起升编码器1通讯故障","4",craneType);
                }
                datas.put("QSBMQ1TXGZ",QSBMQ1TXGZ);

                String QSBMQ2TXGZ=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"起升编码器2通讯故障",false);
                if(QSBMQ2TXGZ.equalsIgnoreCase("true")){
                    this.saveFaultInfo("起升编码器2通讯故障","4",craneType);
                }
                datas.put("QSBMQ2TXGZ",QSBMQ2TXGZ);

                String DCJGCJ1TXGZ=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车激光测距1通讯故障",false);
                if(DCJGCJ1TXGZ.equalsIgnoreCase("true")){
                    this.saveFaultInfo("大车激光测距1通讯故障","4",craneType);
                }
                datas.put("DCJGCJ1TXGZ",DCJGCJ1TXGZ);

                String ZCSDGD=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"整车供电失电",false);
                if(ZCSDGD.equalsIgnoreCase("true")){
                    this.saveFaultInfo("整车供电失电","4",craneType);
                }
                datas.put("ZCSDGD",ZCSDGD);


                String JPTXGZSD=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"机旁通讯故障失电",false);
                if(JPTXGZSD.equalsIgnoreCase("true")){
                    this.saveFaultInfo("机旁通讯故障失电","4",craneType);
                }
                datas.put("JPTXGZSD",JPTXGZSD);

                String JPJTSD=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"机旁急停失电",false);
                if(JPJTSD.equalsIgnoreCase("true")){
                    this.saveFaultInfo("机旁急停失电","4",craneType);
                }
                datas.put("JPJTSD",JPJTSD);


                String DCJGCJ2TXGZ=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车激光测距2通讯故障",false);
                if(DCJGCJ2TXGZ.equalsIgnoreCase("true")){
                    this.saveFaultInfo("大车激光测距2通讯故障","4",craneType);
                }
                datas.put("DCJGCJ2TXGZ",DCJGCJ2TXGZ);

                String DZJGZTYC=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"对中机构状态异常",false);
                if(DZJGZTYC.equalsIgnoreCase("true")){
                    this.saveFaultInfo("对中机构状态异常","4",craneType);
                }
                datas.put("DZJGZTYC",DZJGZTYC);


                String DCDWGZ=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车定位故障",false);
                if(DCDWGZ.equalsIgnoreCase("true")){
                    this.saveFaultInfo("大车定位故障","4",craneType);
                }
                datas.put("DCDWGZ",DCDWGZ);

                String worktopLabel = getWorkTopLabel(craneType);
                String TXGZ=HttpPLCUtils.plcReadWorktopbc(restTemplate,serverHost,craneType,worktopLabel);
                if(TXGZ.equalsIgnoreCase("true")){
                    this.saveFaultInfo(worktopLabel,"4",craneType);
                }
                datas.put("TXGZ",TXGZ);


                simpMessageSendingOperations.convertAndSend("/topic/plcData/"+URLEncoder.encode(craneType, StandardCharsets.UTF_8), JSON.toJSONString(datas));
            }catch (Exception ex){
                log.error(ex.getMessage(),ex);
                ex.printStackTrace();
            }
        }
    }


    private String getWorkTopLabel(String craneType){
        switch (craneType){
            case "11C北":
                return "DJ2通讯故障";
            case "11C南":
                return "DJ3通讯故障";
            case "2#横移":
                return "2号横移通讯故障";
            case "3#横移":
                return "3号横移通讯故障";
            case "4#横移":
                return "4号横移通讯故障";
            case "5#横移":
                return "5号横移通讯故障";
            default:
                return null;
        }
    }

    private void saveFaultInfo(String faultInfo,String faultType,String craneType){
        String nowTask = taskStatus.getWorkStatusByCraneType(craneType).getNowTask();
        FaultRecord faultRecord = FaultRecord.builder().faultType(faultType).additionInfo(faultInfo).craneType(craneType)
                .meetTime(LocalDateTime.now()).workMode("自动".equals(nowTask) ? "自动" : "手动").build();
        AysnSaveFaultInfoThread thread = AysnSaveFaultInfoThread.builder().faultRecord(faultRecord).build();
        ThreadPoolManager.getInstance().execute(thread);
    }


    @Override
    @Scheduled(cron = "0/1 * *  * * ? ")
    @Async
    public void sendTaskData() {
        //获取所有的天车对象
        List<CraneInfo> craneInfoList = craneInfoService.list(null);
        if (craneInfoList.size() > 0) {
            for (CraneInfo craneInfo : craneInfoList) {
                String craneType = craneInfo.getName();
                WorkStatus workStatus = taskStatus.getWorkStatusByCraneType(craneType);
                String memoryStatus = workStatus.getNowStatus();
                String msg;
                try {

                    if("运行中".equals(memoryStatus)){
                        msg = JSON.toJSONString(workStatus);
                    } else {
                        //内存数据不是运行中，则取数据库中的数据进行展示
                        String detailMsg = craneInfo.getDetailMsg();
                        msg = detailMsg;
                        //第一次运行系统，数据库可能无数据
                        if(msg == null){
                            msg = JSON.toJSONString(workStatus);
                        }
                    }

                    simpMessageSendingOperations.convertAndSend("/topic/taskData/" + URLEncoder.encode(craneType, StandardCharsets.UTF_8), msg);
                    //是否发送堆放张数表示复位
                    if(workStatus.isStockNumberFlag()){
                        workStatus.setStockNumberFlag(false);
                    }
                } catch (Exception e) {
                    log.error("/topic/taskData/ 发送数据出现异常",e);

                }
            }
        }
    }

    @Override
    @Scheduled(cron = "0/1 * *  * * ? ")
    @Async
    public void getAndSendTaskOtherData() {
            List<String> craneList = NormalConst.CRANE_LIST;
            for (String craneType : craneList) {
                DeviceStatus deviceStatus = taskOtherStatus.getDeviceStatusByCraneType(craneType);
                try {
                    String craneXVal=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"大车位置",false);
                    deviceStatus.setCraneXVal(craneXVal);
                    String craneZVal= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"起升位置",false);
                    deviceStatus.setCraneZVal(craneZVal);
                    String modeVal= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"模式",false);
                    deviceStatus.setRunMode(modeVal);
                    if(!"5".equals(modeVal)){
                        //模式不为5的情况下，对应已吸取张数归零
                        WorkStatus workStatus = taskStatus.getWorkStatusByCraneType(craneType);
                        workStatus.setSuckedNum("");
                        taskStatus.setWorkStatusByCraneType(craneType,workStatus);
                    }
                    String isFault=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"横移车故障",false);
                    deviceStatus.setIsFault(isFault);

                    CraneInfo craneInfo = craneInfoService.queryByName(craneType);
                    List<Integer> craneIds = new ArrayList<>();
                    craneIds.add(craneInfo.getId());
                    List<Roller> rollerList =rollerService.selectRollerByCraneId(craneIds);
                    if(rollerList!=null&&rollerList.size()>0){
                        String[] lockArr=rollerList.get(0).getLockSignal().split("-");
                        String rollerLock=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,lockArr[0],true);
                        deviceStatus.setRollerLock(rollerLock);

                        String isAllow= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,rollerList.get(0).getAllowSignal(),true);
                        deviceStatus.setRollerReady(isAllow);
                    }

                    String suckSteel=HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"吊具有板",false);
                    deviceStatus.setSuckSteel(suckSteel);
                    if(!"2#横移".equals(craneType) && !("3#横移").equals(craneType)){
                        String selectSignal= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,rollerList.get(0).getSelectSignal(),true);
                        deviceStatus.setSelectSignal(selectSignal);
                    }
                    if("11C北".equals(craneType) || ("11C南").equals(craneType)){
                        String steelExceed= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"钢板超位检测",false);
                        deviceStatus.setSteelExceed(steelExceed);
                    }

                    simpMessageSendingOperations.convertAndSend("/topic/taskOtherData/" + URLEncoder.encode(craneType, StandardCharsets.UTF_8), JSON.toJSONString(deviceStatus));
                } catch (Exception e) {
                    log.error(e.getMessage(),e);
                    e.printStackTrace();
                }
            }
    }



    @Override
    @Scheduled(cron = "0/3 * *  * * ? ")
    @Async
    public void refreshCraneStatus() {
        try {
            List<String> craneList = NormalConst.CRANE_LIST;
            for (String s : craneList) {
                int status=0;
                //从PLC中获取天车故障信息；
                String isError=HttpPLCUtils.plcReadbc(restTemplate,serverHost,s,"横移车故障",false);
                if("TRUE".equalsIgnoreCase(isError)){
                    //TODO 如果从一级读辊道锁定为true，则释放辊道写false
                    status=1;
                }else {
                    status=0;
                }
                craneWorkFactory.getCraneWorkImpl(s).getData().setCraneStatus(status);
                if(status!=0){  //天车故障，停止自动任务；  0为正常，非0为故障
                    taskRedisApi.set(s+"_status",status);
                    craneWorkFactory.getCraneWorkImpl(s).getData().setStartLogo(false);
                }

            }
        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            ex.printStackTrace();
        }
    }

}
